package com.hbt.springbootquick.service;

import com.hbt.springbootquick.dao.HyUserMapper;
import com.hbt.springbootquick.dao.YzMapper;
import com.hbt.springbootquick.entity.HyUser;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MyService {
    @Autowired
    HyUserMapper hyUserMapper;

    public void insertData(String req_data_json, List<String> yzs_result){
        JSONObject object = JSONObject.fromString(req_data_json);
        HyUser hyUser = new HyUser();

        JSONArray array = object.getJSONArray("yz_data");
        int grade = 0;
        for (int i = 0; i < array.length();i++){
            JSONObject a_object = array.getJSONObject(i);
            boolean win = false;
            for (int j = 0; j < yzs_result.size();j++){
                if (yzs_result.get(j).equals(a_object.getString("name"))){
                    win = true;
                }
            }
            if (win){
                grade += a_object.getInt("num");
            }else grade -= a_object.getInt("num");
        }
        //转化为bean
        hyUser.setUserNick(object.getString("userNick"));
        hyUser.setGrade(grade);
        hyUser.setUserUnionId(object.getString("userUnionId"));
        //插入数据库
        hyUserMapper.insertHyUser(hyUser);

    }
}
